package countingWords;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Scanner;
public class CountingWords {
	//create the map 
	public static void createMap(Map<String, Integer> map) 
	{
	   Scanner scanner = new Scanner(System.in); // create scanner
	   System.out.println("pleas enter a string:"); // suggest user input some words
	   String input = scanner.nextLine();
	 
	   // split them
	   String[] temWords = input.split(" ");
	            
	   //deal with words 
	   for (String tem : temWords) 
	   {
	      String word = tem.toLowerCase(); // get lowercase word
	               
	      // if the map contains the word
	     if(word.equals(""))
	     {
	    	 break;
	     }
	     else if (map.containsKey(word)) // is word in map
	      {
	         int count = map.get(word); // get current count
	         map.put(word, count + 1); // increment count
	      } 
	      else 
	         map.put(word, 1); // add new word with a count of 1 to map
	   } 
	} 
	 
	// display map content
	public static void displayMap(Map<String, Integer> map) 
	{     
	   Set<String> keys = map.keySet(); // put them into set to get keys
	 
	   // put the set into treeset to sort them
	   TreeSet<String> sortedKeys = new TreeSet<>(keys);
	 
	   System.out.printf("%nMap contains:%nKey\t\tValue%n");
	   
	  Iterator<String>  iter = sortedKeys.iterator();
	   // generate output for each key in map
	  while(iter.hasNext()) {
		  String key = (String)iter.next();
	      System.out.printf("%-7s%10s%n", key, map.get(key));
	  }
	  
	  System.out.println("is it empty?" + map.isEmpty());
	}
	
}
